<?php
namespace Home\Controller;
class TribeController extends CommonController{
	/*获取部落列表*/
	public function getTribeList(){
		$limit = empty($_POST['pageSize'])?30:intval($_POST['pageSize']);
        $page = empty($_POST['pageCurrent'])?1:intval($_POST['pageCurrent'])+1;
        $mustData = array(
			'userId'=>$_POST['userId'],
		);
		CheckMustData($mustData);
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}

        $tribe_model = M("Tribe");
        $condition = array();
        $count = $tribe_model->where($condition)->count();
        $dataList = $tribe_model->where($condition)->limit($limit)->page($page)->order("tr_trophy_num desc")->select();
        foreach ($dataList as $key => $value) {
        	$tribeConfig = $this->getTribeConfig();
        	$dataList[$key]['tr_member_num_max'] = $tribeConfig['sc_value'];
        	//
        	$tribe_level = M("TribeLevel")->where(array('tl_level'=>$value['tr_level']))->find();
        	$dataList[$key]['max_get_cloude_money'] = 0;
			if(!empty($tribe_level)){
				$dataList[$key]['max_get_cloude_money'] = $tribe_level['tl_get_cloude_1'];
			}
			$dataList[$key]['max_get_cloude_money'] *= 30;
        }
        $resultData = array(
        	'count'=>$count,
        	'dataList'=>$dataList,
        	'hasNickname'=>empty($userInfo['us_nickname'])?0:1,
        	'hasTride'=>($userInfo['us_tribe_id']!=null&&$userInfo['us_tribe_status']==2)?$userInfo['us_tribe_id']:'',
        );
        ApiResult(200,$resultData);
	}

	/* 更改部落成员级别 */
	public function editTribeMemberLevel(){
		$mustData = array(
			'userId'=>$_POST['userId'],//用户ID
			'memberId'=>$_POST['memberId'],//成员ID
			'level'=>$_POST['level'],//0成员 1副首领
		);
		CheckMustData($mustData);
		
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}
		$memberId = M("User")->find($_POST['memberId']);
		if(empty($memberId)){
			ApiResult(306);
		}
		if($userInfo['us_tribe_id']!=$memberId['us_tribe_id']||$userInfo['us_tribe_level']!=2||$memberId['us_tribe_level']==2){
			ApiResult(604);
		}
		if($_POST['level']!=0&&$_POST['level']!=1){
			ApiResult(301);
		}
		$saveData = array(
			'us_tribe_level'=>$_POST['level'],
		);
		if($_POST['level']==1){
			$tribeConfig = $this->getTribeConfig();
        	$condition = array(
        		'us_tribe_id'=>$memberId['us_tribe_id'],
        		'us_tribe_status'=>2,
        		'us_tribe_level'=>1,
        	);
        	$trideList = M("User")->field("us_id")->where($condition)->select();
        	if(count($trideList)>=$tribeConfig['sc_value_2']){
        		ApiResult(606);
        	}
			$saveData['us_tribe_next_time']=strtotime(date('Y-m-d 23:59:59', time()));//+1*24*60*60
		}
		$res = M("User")->where(array('us_id'=>$memberId['us_id']))->save($saveData);
		if($res){
			ApiResult(200);
		}else{
			ApiResult(300);
		}
	}

	/* 移除部落成员 */
	public function delTribeMember(){
		$mustData = array(
			'userId'=>$_POST['userId'],//用户ID
			'memberId'=>$_POST['memberId'],//成员ID
		);
		CheckMustData($mustData);
		
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}
		$memberId = M("User")->find($_POST['memberId']);
		if(empty($memberId)){
			ApiResult(306);
		}
		if($userInfo['us_tribe_id']!=$memberId['us_tribe_id']||$userInfo['us_tribe_level']!=2||$memberId['us_tribe_level']==2){
			ApiResult(604);
		}
		$saveData_u = array(
    		'us_tribe_id'=>array("exp",'null'),
    		'us_tribe_status'=>0,
    		'us_tribe_next_time'=>array("exp",'null'),
    		'us_tribe_level'=>array("exp",'null'),
    	);
    	$res = M("User")->where(array('us_id'=>$memberId['us_id']))->save($saveData_u);
		if($res){
			M("Tribe")->where(array('tr_id'=>$userInfo['us_tribe_id']))->setDec("tr_member_num",1);
			ApiResult(200);
		}else{
			ApiResult(300);
		}
	}

	/* 部落成员退出 */
	public function quitTribe(){
		$mustData = array(
			'userId'=>$_POST['userId'],//用户ID
		);
		CheckMustData($mustData);
		
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}
		if($userInfo['us_tribe_level']==2){
			ApiResult(607);
		}
		$saveData_u = array(
    		'us_tribe_id'=>array("exp",'null'),
    		'us_tribe_status'=>0,
    		'us_tribe_next_time'=>array("exp",'null'),
    		'us_tribe_level'=>array("exp",'null'),
    	);
    	$res = M("User")->where(array('us_id'=>$userInfo['us_id']))->save($saveData_u);
		if($res){
			M("Tribe")->where(array('tr_id'=>$userInfo['us_tribe_id']))->setDec("tr_member_num",1);
			ApiResult(200);
		}else{
			ApiResult(300);
		}
	}

	/* 解散部落 */
	public function dissolveTribe(){
		$mustData = array(
			'userId'=>$_POST['userId'],//用户ID
		);
		CheckMustData($mustData);
		
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}
		if($userInfo['us_tribe_level']!=2){
			ApiResult(604);
		}
		$saveData_u = array(
    		'us_tribe_id'=>array("exp",'null'),
    		'us_tribe_status'=>0,
    		'us_tribe_next_time'=>array("exp",'null'),
    		'us_tribe_level'=>array("exp",'null'),
    	);
    	$res = M("User")->where(array('us_tribe_id'=>$userInfo['us_tribe_id']))->save($saveData_u);
		if($res){
			M("Tribe")->where(array('tr_id'=>$userInfo['us_tribe_id']))->delete();
			ApiResult(200);
		}else{
			ApiResult(300);
		}
	}
	/* 发布公告 */
	public function setNotice(){
		$mustData = array(
			'userId'=>$_POST['userId'],//用户ID
			'notice'=>$_POST['notice'],//公告内容
		);
		CheckMustData($mustData);
		
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}
		if($userInfo['us_tribe_level']!=2){
			ApiResult(604);
		}
		$saveData = array(
    		'tr_notice'=>$_POST['notice'],
    	);
    	M("Tribe")->where(array('tr_id'=>$userInfo['us_tribe_id']))->save($saveData);
		ApiResult(200);
	}

	//获取部落信息
	public function getTribeInfo(){
		$mustData = array(
			'userId'=>$_POST['userId'],
			'tr_id'=>$_POST['tr_id'],
		);
		CheckMustData($mustData);
		$tribeInfo = M("Tribe")->find($_POST['tr_id']);
		if(empty($tribeInfo)){
			ApiResult(603);
		}
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}
		$condition = array(
			'us_tribe_id'=>$_POST['tr_id'],
			'us_tribe_status'=>array('not in',array(1,3)),
		);
		$tribeInfo = D("Tribe")->getTribeInfoById($_POST['tr_id'],$_POST['userId']);
		ApiResult(200,$tribeInfo);
	}
	//领取部落云币
	public function getTribeCloudMoney(){
		$mustData = array(
			'userId'=>$_POST['userId'],
			'tr_id'=>$_POST['tr_id'],
		);
		CheckMustData($mustData);
		$tribeInfo = M("Tribe")->find($_POST['tr_id']);
		if(empty($tribeInfo)){
			ApiResult(603);
		}
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}
		if($userInfo['us_tribe_id']==$tribeInfo['tr_id']&&$tribeInfo['tr_level']>0){
			if(time()>$userInfo['us_tribe_next_time']){
				$tribe_level = M("TribeLevel")->where(array('tl_level'=>$tribeInfo['tr_level']))->find();
				if(!empty($tribe_level)){
					$saveData = array(
						'us_cloud_money'=>$userInfo['us_cloud_money']+$tribe_level['tl_get_cloude_'.$userInfo['us_tribe_level']],
						'us_tribe_next_time'=>strtotime(date('Y-m-d 23:59:59', time())),
					);
					M("User")->where(array('us_id'=>$_POST['userId']))->save($saveData);

					$awardLogData = array(
						'aw_value'=>$tribe_level['tl_get_cloude_'.$userInfo['us_tribe_level']],
						'aw_target'=>$userInfo['us_id'],
						'aw_source'=>$userInfo['us_id'],
						'aw_create_time'=>time(),
						'aw_type'=>3
					);
					M()->table(C('DB_PREFIX').'award_log')->add($awardLogData);

					ApiResult(200);
				}
			}
		}
		ApiResult(300);
	}
	//创建部落
	public function createTribe(){
		$mustData = array(
			'userId'=>$_POST['userId'],
			'tr_name'=>$_POST['tr_name'],
		);
		CheckMustData($mustData);
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}
		if($userInfo['us_tribe_id']!=null&&$userInfo['us_tribe_status']!=3&&$userInfo['us_tribe_status']!=1){
			ApiResult(601);
		}
		$tribe_data = array(
			'tr_name'=>htmlspecialchars($_POST['tr_name']),
			'tr_level'=>0,
			'tr_create_user'=>$_POST['userId'],
			'tr_create_time'=>time(),
			'tr_member_num'=>1,
		);
		$tribe_id = M("Tribe")->add($tribe_data);
		$user_data = array(
			'us_tribe_id'=>$tribe_id,
			'us_tribe_level'=>2,
			'us_tribe_next_time'=>strtotime(date('Y-m-d 23:59:59', time())),
			'us_tribe_status'=>2,
		);
		M("User")->where(array('us_id'=>$_POST['userId']))->save($user_data);
		ApiResult(200,array('tr_id'=>$tribe_id));
	}

	/*申请部落*/
	public function applyTribe(){
		$mustData = array(
			'userId'=>$_POST['userId'],
			'tr_id'=>$_POST['tr_id'],
		);
		CheckMustData($mustData);
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}
		/*if($userInfo['us_tribe_id']!=null&&$userInfo['us_tribe_status']!=3){
			ApiResult(602);
		}*/
		$tribeInfo = M("Tribe")->find($_POST['tr_id']);
		if(empty($tribeInfo)){
			ApiResult(603);
		}
		$tribeConfig = $this->getTribeConfig();
		if($tribeInfo['tr_member_num']>=$tribeConfig['sc_value']){
			ApiResult(605);
		}
		$user_data = array(
			'us_tribe_id'=>$_POST['tr_id'],
			'us_tribe_status'=>1,
		);
		M("User")->where(array('us_id'=>$_POST['userId']))->save($user_data);
		ApiResult(200);
	}
	/* 获取申请列表 */
	public function applyTribeUser(){
		$mustData = array(
			'userId'=>$_POST['userId'],
			'tr_id'=>$_POST['tr_id'],
		);
		CheckMustData($mustData);
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)){
			ApiResult(306);
		}
		if($userInfo['us_tribe_id']!=$_POST['tr_id']){
			ApiResult(604);
		}
		$tribeInfo = M("Tribe")->find($_POST['tr_id']);
		if(empty($tribeInfo)){
			ApiResult(603);
		}
		$condition = array();
		$condition['us_tribe_id'] = $_POST['tr_id'];
		$condition['us_tribe_status'] = 1;
		$data = M("User")->where($condition)->select();
		ApiResult(200,array('dataList'=>$data));
	}
	/* 审核申请 */
	public function auditingApplyTribe(){
		$mustData = array(
			'userId'=>$_POST['userId'],
			'apply_user'=>$_POST['apply_user'],
			'status'=>$_POST['status'],//2同意 3拒绝
		);
		
		$apply_user = M("User")->find($_POST['apply_user']);
		$userInfo = M("User")->find($_POST['userId']);
		if(empty($userInfo)||empty($apply_user)){
			ApiResult(306);
		}
		if($apply_user['us_tribe_id']!=$userInfo['us_tribe_id']||(intval($userInfo['us_tribe_level'])!=2||intval($apply_user['us_tribe_status'])!=1)){
			ApiResult(604);
		}

		$user_data = array();
		if($_POST['status']==2){
			$tribeInfo = M("Tribe")->find($userInfo['us_tribe_id']);
			$tribeConfig = $this->getTribeConfig();
			if($tribeInfo['tr_member_num']>=$tribeConfig['sc_value']){
				ApiResult(605);
			}
			$user_data = array(
				'us_tribe_status'=>2,
				'us_tribe_level'=>0,
				'us_tribe_next_time'=>strtotime(date('Y-m-d 23:59:59', time())),
			);
			M("Tribe")->where(array('tr_id'=>$userInfo['us_tribe_id']))->setInc("tr_member_num",1);
		}else{
			$user_data = array(
				'us_tribe_status'=>3,
			);
		}
		M("User")->where(array('us_id'=>$_POST['apply_user']))->save($user_data);
		ApiResult(200);
	}

	/* 获取部落配置信息 */
	public function getTribeConfig(){
		$sysconfig = C('sysconfig');
		$condition['sc_id'] = $sysconfig['tribe'];
		$data = M("SystemConfig")->where($condition)->find();
		return $data;
	}

}